from django.db import models
from  user.models import  User
import  os
# Create your models here.
def save_file(instance,filename):
    # 返回文件的保存路径，并把这个路径写入数据库中对应的字段中
    return os.path.join('video',filename)

def save_img(instance,filename):
    # 返回文件的保存路径，并把这个路径写入数据库中对应的字段中
    return os.path.join('static','img',filename)
class Category(models.Model): # 课程分类表
    class Meta():
        verbose_name = verbose_name_plural = '课程种类表'
    cat_name = models.CharField(max_length=50, verbose_name='课程分类表')
    def __str__(self):
        return self.cat_name

class Course(models.Model): #课程表
    class Meta():
        verbose_name = verbose_name_plural = '课程表'
    course_name = models.CharField(max_length=20,verbose_name='课程名称')
    sender = models.ForeignKey(to = Category, on_delete=models.CASCADE, related_name='sender_set',verbose_name='课程分类') #关联课程分类表
    buyer = models.ManyToManyField(to = User,related_name='buyer_set' ,verbose_name='购买用户',blank=True) #购买课程用户
    carshop = models.ManyToManyField(to = User, related_name='carshop_set',verbose_name='购物车用户',blank=True) #购物车用户
    # img = models.CharField(max_length=255,verbose_name='缩略图')
    img = models.ImageField(upload_to=save_img,verbose_name='缩略图')
    # video = models.CharField(max_length=355, verbose_name='视频')
    video = models.FileField(upload_to=save_file,verbose_name='视频路径')
    price =models.DecimalField(max_digits=12,decimal_places=2,default=0,verbose_name='售价')
    jieshao =models.TextField(blank=True)

